<template>
  <div class="about">
    <!--搜索查询-->
    <el-row>
      <el-col :span="3">&nbsp;</el-col>
      <el-col :span="3"
        ><el-input
          v-model="username"
          size="mini"
          placeholder="请输登录名称"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-input
          v-model="name"
          size="mini"
          placeholder="请输用户名"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-input v-model=" email" size="mini" placeholder="邮箱"></el-input
      ></el-col>
      <el-col :span="3"
        ><el-input
          v-model="contactPhone"
          size="mini"
          placeholder="电话"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="search()"
          icon="el-icon-search"
          v-has="'member:query'"
          >搜索</el-button
        ></el-col
      >
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="addFormVisible = true"
          icon="el-icon-circle-plus"
          v-has="'member:add'"
          >添加</el-button
        ></el-col
        >
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="flush()"
          icon="el-icon-search"
          >刷新</el-button
        ></el-col
      >
    </el-row>
    <!--table分页列表数据-->
    <el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="username" label="登录账号"> </el-table-column>
        <el-table-column prop="name" label="客户名字"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="contactPhone" label="电话"> </el-table-column>
        <el-table-column prop="sex" :formatter="getSexName" label="性别">
        </el-table-column>
        <el-table-column label="头像">
          　<template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.headPic"
            ></el-image>
          </template>
          <!--<img :src="avatar" class="userAvatar"
                             style="display: block;width: 50px;height: 50px;
                         border-radius: 50%;-webkit-border-radius: 50%;
                         -moz-border-radius: 50%;
                         border: 4px solid #44576b;box-sizing: border-box;margin: 10px auto 10px;">-->
        </el-table-column>
        <el-table-column prop="status" :formatter="getStatusName" label="状态">
        </el-table-column>
        <el-table-column prop="loginDate" label="最后一次登录时间">
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="toEdit(scope.$index, scope.row)"
              v-has="'member:modify'"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              v-has="'member:delete'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!--分页-->
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10, 100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
  <!--添加弹出框-->
    <el-row>
      <el-dialog title="添加" :visible.sync="addFormVisible">
        <el-form :model="form">
          <el-form-item label="登录账号" :label-width="formLabelWidth">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="客户名字" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth">
            <el-input v-model="form.contactPhone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-radio v-model="form.sex" label="0">男</el-radio>
            <el-radio v-model="form.sex" label="1">女</el-radio>
            <el-radio v-model="form.sex" label="2">未知</el-radio>
          </el-form-item>
          <el-form-item label="头像上传" :label-width="formLabelWidth">
            <!--图片上传-->
            <el-upload
              class="avatar-uploader"
              action="http://localhost:23232/member/uploadUserHeadPic?accessToken"
              name="file"
              :auto-upload="true"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input
              v-model="form.password"
              autocomplete="off"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add()">确 定</el-button>
        </div>
      </el-dialog>
    </el-row>
<el-row>
  <!--更新弹出框-->
  <el-dialog title="更新" :visible.sync="updateFormVisible">
        <el-form :model="form">
          <el-form-item label="登录账号" :label-width="formLabelWidth">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="客户名字" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth">
            <el-input v-model="form.contactPhone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-radio v-model="form.sex" label="0">男</el-radio>
            <el-radio v-model="form.sex" label="1">女</el-radio>
            <el-radio v-model="form.sex" label="2">未知</el-radio>
          </el-form-item>
          <el-form-item label="头像上传" :label-width="formLabelWidth">
            <!--图片上传-->
            <el-upload
              class="avatar-uploader"
              action="/member/uploadUserHeadPic"
              name="file"
              :auto-upload="true"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input
              v-model="form.password"
              autocomplete="off"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="update()">确 定</el-button>
        </div>
      </el-dialog>
</el-row>

   
  </div>
</template>
<script>
export default {
  data() {
    return {    
      username:"",name:"",phoneNumber:"",contactPhone:""  ,
      Id:"",
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 20,
      addFormVisible: false, //添加对话框是否显示
      updateFormVisible: false, //更新对话框是否显示
      rolesVisible: false, //是否显示角色对话框
      form: { user_type: "0", sex: "0", status: "0", email: "" }, //添加或者更新form
      formLabelWidth: "120px",
      imageUrl: "",
      checkedRoles: [], //选中对象列表
      userId: "", //用户编号
      isIndeterminate: true,
      roleIds: "", //选中的节点的ID字符串
    };
  },


 created: function () {
    this.loadData();
    //初始化获取所有角色数据
    var _this = this;
    //初始化数据，加载所有的角色
    this.$http
      .post("/member/queryAll", {
        pageNo: 1,
        pageSize: 1000,
        isHaveSatus: 1,
      })
      .then(function (response) {
        _this.roles = response.data.data.list;
      });
  },
  methods: {
    //加载数据
    loadData() {
      var _this = this;
      //初始化数据
      _this.$http
        .post("/member/queryAll", {
          pageNo: this.currentPage,
          pageSize: this.pageSize,
           username: this.username
          ,name:this.name
          ,phoneNumber:this.phoneNumber
          ,contactPhone: this.contactPhone ,
        })
        .then(function (response) {
          // handle success  处理成功，回调的方法
          //console.log(response);
          //赋值分页列表
          if (response.data.code == 200) {
            _this.tableData = response.data.data.list;
            //删除最后一页为空时，调到上一页
            //alert(_this.tableData.length);
            if (_this.tableData.length == 0 && _this.currentPage != 1) {
              _this.currentPage = _this.currentPage - 1;
              _this.loadData();
            }
            //赋值总条数
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          // handle error 处理异常
          console.log(error);
        })
        .then(function () {
          // always executed  总是执行  相当于finally
        });
    },
    //查询
    search() {
      this.loadData();
    },
    //每页数量发生变化时
    handleSizeChange(v) {
      this.pageSize = v;
      this.loadData();
    },
    //当前页数放生变化是
    handleCurrentChange(v) {
      this.currentPage = v;
      this.loadData();
    },
    //添加
    add: function () {
      var _this = this;
      this.$http
        .put("/member/add", this.form)
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            //提示成功
            _this.$message({
              message: "恭喜你，添加成功",
              type: "success",
            });
            //_this.$message.success("恭喜你，添加成功");
            //关闭弹出框
            _this.addFormVisible = false;
            //重新加载数据
            _this.loadData();
            //清空form
            _this.form = {};
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    //用户性别
    getSexName(row, column, cellValue) {
      return cellValue == 0 ? "男" : cellValue == 1 ? "女" : "未知";
    },
    //账户状态名字
    getStatusName(row, column, cellValue) {
      return cellValue == 0 ? "正常" : "禁用";
    },
    //跳转更新
    toEdit(index, row) {
      //清空原来的
      this.form.avatar = "";
      this.form = row;
      this.updateFormVisible = true;
    },
    //更新
    update: function () {
      var _this = this;
      //JSON.stringify JSON 转 String
      // alert(JSON.stringify(this.form));
      this.$http
        .put("/member/update", this.form)
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            //提示成功
            _this.$message({
              message: "恭喜你，操作成功",
              type: "success",
            });
            //_this.$message.success("恭喜你，添加成功");
            //关闭弹出框
            _this.updateFormVisible = false;
            //重新加载数据
            _this.loadData();
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    //删除
    handleDelete(index, row) {
      var _this = this;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          _this.$http
            .delete("/member/deleteById?id=" + row.id)
            .then(function (response) {
              console.log(response);
              if (response.data.code == 200) {
                //提示成功
                _this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                //重新加载数据
                _this.loadData();
              }
            })
            .catch(function (error) {
              console.log(error);
              _this.$message.error("你错了。。。");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //图片上传成功后
    handleAvatarSuccess(res, file) {
      //图片回显
      this.imageUrl = URL.createObjectURL(file.raw);

      //   alert(file.response);
      //alert(JSON.stringify(file));
      // alert(JSON.stringify(file.response));
      this.form.headPic = file.response.data.filePath;
      console.log("上传文件原始名称为:" + file.response.data.fileOldName);
      // this.form.fileName=file.response.oldFileName;
      // this.form.filePath=file.response.newFilePath;
    },
    //上传前图片类型和大小判断
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      /* if (!isJPG) {
                          this.$message.error('上传头像图片只能是 JPG 格式!');
                      }*/
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    //刷新当前页
    flush() {
      location.reload();
    },
  },
};
</script>
 
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>